<template>

    <el-container class="home-container">
      <!-- 头部区域 -->
      <el-header>

         <img src="../assets/icon.jpg" style="" alt="">
        <span>电商管理系统</span>

         <el-button type="info" @click="logout">退出</el-button>
      </el-header>
      <!-- 页面主体 -->
      <el-container>
       <!-- 侧边栏 -->
        <el-aside :width="isCollapse? '64px' :'200px'">
          <div class="toggle-button" @click="toggleCollapse">{{isCollapse? '展开' :'折叠'}}</div>
           <!-- 侧边菜单区域 -->
          <el-menu

                class="el-menu-vertical-demo"
                background-color="#545c64"
                text-color="#fff"
                active-text-color="#ffd04b"
                unique-opened
                :collapse="isCollapse"
                :collapse-transition="false"
                :default-active="activePath"
                router
                >
                <!-- 一级菜单 -->
            <el-submenu :index="item.id +''" v-for="item in menuList" :key="item.id">
                  <template slot="title">
                    <i :class="iconObj[item.id]"></i>
                    <span>{{item.authName}}</span>
                  </template>
                  <!-- 二级菜单 -->
                    <el-menu-item :index="'/'+subItem.path +''"
                     v-for="subItem in item.children" :key="subItem.id"
                     @click="saveNavState('/'+subItem.path +'')">
                      <template slot="title">
                        <i class="el-icon-menu"></i>
                       {{subItem.authName}}
                        </template>
                       </el-menu-item>

              </el-submenu>

        </el-menu>
      </el-aside>
        <!-- 主体区域 -->
        <el-main>
          <!-- 路由占位符 -->
           <router-view></router-view>
        </el-main>
      </el-container>

    </el-container>

</template>
<script>
export default {
  created () {
    this.getMenuList()
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  data () {
    return {
      menuList: [],
      iconObj: {
        125: 'iconfont icon-user',
        103: 'iconfont icon-tijikongjian',
        101: 'iconfont icon-shangpin',
        102: 'iconfont icon-danju',
        145: 'iconfont icon-baobiao'

      },
      // 是否折叠
      isCollapse: false,
      activePath: ''
    }
  },
  methods: {
    logout () {
      window.sessionStorage.clear()
      this.$router.push('/login')
      this.$message('退出成功')
    },
    async getMenuList () {
      const { data: res } = await this.$http.get('menus')
      if (res.meta.status !== 200) return this.$message.error('res.meta.msg')
      this.menuList = res.data
      // 导航列表
      // console.log(res)
    },
    // 菜单额折叠与展开
    toggleCollapse () {
      // this.collapse:ture
      this.isCollapse = !this.isCollapse
    },
    saveNavState (activePath) {
      // 菜单高亮
      window.sessionStorage.setItem('activePath', activePath)
      this.activePath = activePath
    }

  }
}
</script>

<style lang="less" scoped>
  .home-container{
    height: 100%;
    width: 100%;

  }
   .el-header{
    background: #33373f;
    display: flex;
    justify-content: space-between;
    padding-left: 0;
    align-items: center;

    >img{

      width: 50px;
      height: 50px;
      border-radius: 50%;

    }
    >span{
      align-content: center;
      font-size: 30px ;
      color: rgb(248, 248, 248);
    }
  }
  .el-footer {
    background: #373d41;
  }

  .el-aside {
    background: #373d41;
   .el-menu{
      border-right: none;
    }
  }

  .el-main {
    background-color: #E9EEF3;
  }
  .iconfont{
    margin-right: 10px;
  }
  .toggle-button{
    background-color:#4a5064;
    font-size: 15px;
    line-height: 24px;
    color: #fff;
    text-align: center;
    letter-spacing: 0.2em;
    cursor:pointer;

  }

</style>
